package com.jack.payment.controller.api;


import com.jack.payment.entity.business.ApplyRefundBo;
import com.jack.payment.entity.business.RequestPayBo;
import com.jack.payment.entity.common.BusinessException;
import com.jack.payment.entity.common.CommonResult;
import com.jack.payment.entity.common.PayCallParam;
import com.jack.payment.entity.generate.user.UserApiModel;
import com.jack.payment.service.strategy.PaymentContext;
import com.jack.payment.util.redis.GenerateId;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Tag(name = "支付调用")
@Controller
@RequestMapping("/api/v1/pay")
public class PaymentController {
    private Logger log = LoggerFactory.getLogger(PaymentController.class);

    @Autowired
    private PaymentContext paymentContext;
    @Autowired
    private GenerateId generateId;


    @Operation(summary = "支付调用")
    @RequestMapping(value = "getTransactionDetail", method = {RequestMethod.POST})
    @ResponseBody
    public CommonResult getTransaction(@RequestBody @Validated PayCallParam<RequestPayBo> data) throws BusinessException {
        RequestPayBo requestPayBo = data.getData();
        UserApiModel userApiModel = (UserApiModel) data.getApiKey();
        requestPayBo.setOrderNo(generateId.generateOrderId());
        return CommonResult.ok(paymentContext.createPaymentInfo(requestPayBo, userApiModel));
    }

    @Operation(summary = "退款调用")
    @RequestMapping(value = "refund", method = {RequestMethod.POST})
    @ResponseBody
    public CommonResult refunds(@RequestBody @Validated PayCallParam<ApplyRefundBo> data) throws BusinessException {
        ApplyRefundBo applyRefundBo = data.getData();
        UserApiModel userApiModel = (UserApiModel) data.getApiKey();
        paymentContext.applySingleRefund(applyRefundBo, userApiModel);
        return CommonResult.ok();
    }

    @Operation(summary = "查看订单详情")
    @RequestMapping(value = "getOrderDetail", method = {RequestMethod.POST})
    @ResponseBody
    public CommonResult getOrderDetail() {

        return CommonResult.ok();
    }
}
